package com.epam.facebook_client.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.epam.facebook_client.AlbumsActivity;
import com.epam.facebook_client.R;
import com.epam.facebook_client.content.contract.AppContract;
import com.epam.facebook_client.content.db.DbOpenHelper;
import com.epam.facebook_client.manager.ImageManager;

public class ProfileFragment extends Fragment {

	private static final String TAG = ProfileFragment.class.getSimpleName();
	private ImageManager imageManager;
	private String id;
	private FragmentActivity fragmentActivity;
	private Button openAlbumButton;
	private View inflate;

	public static ProfileFragment newInstance(String id) {
		ProfileFragment pa = new ProfileFragment();
		Bundle args = new Bundle();
		args.putString("id", id);
		pa.setArguments(args);
		return pa;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		inflate = inflater.inflate(R.layout.activity_profile, container, false);
		fragmentActivity = getActivity();
		openAlbumButton = (Button) inflate.findViewById(R.id.openAlbum);
		imageManager = ImageManager.get(fragmentActivity);

		id = getArguments().getString("id");

		openAlbumButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(fragmentActivity, AlbumsActivity.class);
				intent.putExtra("id", id);
				startActivity(intent);
			}
		});
		Cursor cursor = getActivity().getContentResolver().query(AppContract.FriendsColumns.CONTENT_URI,
				DbOpenHelper.FRIENDS_COLUMNS, "_id = ?", new String[] { id }, null);
		cursor.moveToFirst();
		if (cursor.getCount() > 0) {
			viewResult(cursor);
		}
		return inflate;
	}

	private void viewResult(Cursor cursor) {
		String[] columns = { AppContract.FriendsColumns.NAME, AppContract.FriendsColumns.GENDER,
				AppContract.FriendsColumns.BIRTHDAY, AppContract.FriendsColumns.BIO,
				AppContract.FriendsColumns.PICTURE };
		int[] viewId = { R.id.profileName, R.id.profileGender, R.id.profileBirthday, R.id.profileBio,
				R.id.profileImage };

		for (int i = 0; i < viewId.length; i++) {
			View view = inflate.findViewById(viewId[i]);
			if (view instanceof TextView) {
				TextView textView = (TextView) view;
				String data = cursor.getString(cursor.getColumnIndex(columns[i]));
				if (data != null && data != "") {
					textView.setVisibility(View.VISIBLE);
					textView.setText(data);
				} else {
					textView.setVisibility(View.GONE);
				}
			}

			if (view instanceof ImageView) {
				ImageView imageView = (ImageView) view;
				String data = cursor.getString(cursor.getColumnIndex(columns[i]));
				if (data != null && data != "") {
					imageView.setVisibility(View.VISIBLE);
					imageManager.loadImage(data, imageView);
				} else {
					imageView.setVisibility(View.GONE);
				}
			}
		}
	}
}